//暂停视频
function paser(){
    var video = document.getElementsByTagName("video");
    var len = video.length;
    if(len>0){
    	for(var i = 0 ; i <len;i++){
    		 video[i].pause();
    	}
    }
}
$(function(){
	//分类的数据 右边
	$.ax(
		"/tk/api/resource/getAllTypeResource",
		null,
		null,
		"get",
		"json",
		function(data){
			var str = "";
				var obj = data.data;
				for(var i = 0 ; i <obj.length;i++){
					str = str + "<li type-id='"+obj[i].id+"'><img class='left-btn' src='/tk/img/Bleft-btn.png'/><span>"+obj[i].name+"("+obj[i].sum+")</span></li>";
				}
				$(".btn-lists").append("<li class='btn-active' id='allList'><img class='left-btn' src='/tk/img/Aleft-btn.png'/><span>全部("+data.sum+")</span></li><div style='clear:both'></div>");
				$(".btn-lists").append(str);
		},
		function(){
			alert("error");
		}
	);
	//默认左边的列表数据 
	function getLeftList(){
		$.ax(
			"/tk/api/resource/getAllResource",
			null,	//发送的数据
			null,
			"get",
			"json",
			function(data){
				var str = "";
					var obj = data;
					for(var i = 0 ; i <obj.length;i++){
						var time = getMyDate(obj[i].createtime);
						var intro = obj[i].introduction;
						if(intro=="null"||intro==undefined||intro==null||intro==""){
							intro="";
						}else{
							var strIndex = intro.indexOf("。");
							if(strIndex!=-1){
								intro = intro.substring(0, strIndex+1);
							}
						}
						
						if(obj[i].resource_type==1){
							//调用tools中的转换时间格式			
							str = str + "<li data-toggle='modal' data-target='.bs-example-modal-lg' context-id='"+obj[i].id+"'><p class='title-text'>"+obj[i].title+"</p><p class='content-text'>"+intro+"</p><p class='text-right small'>"+time+"</p>";
						}else if(obj[i].resource_type==3){
							str = str + "<li data-toggle='modal' data-target='.bs-example-modal-lg' class='row' context-id='"+obj[i].id+"'><img class='col-xs-12 col-sm-9 col-lg-9 col-md-9 left-img'  src='"+obj[i].resourceurl+"'</><div class='right-box col-xs-12 col-sm-3 col-lg-3 col-md-3 '><p class='title-text'>"+obj[i].title+"</p><p small'>"+time+"</p><p class='content-text'>"+intro+"</p></div>";
						}
					}
					$(".lists-ul").html(str);

			},
			function(){
				alert("error");
			}
		);
	}
	getLeftList();
	//点击右边分类，左边相应改变列表
	$(".btn-lists").on("click","li",function(index){	
		var typeId = $(this).attr("type-id");
		$(this).addClass("btn-active").siblings().removeClass("btn-active");
		$(this).children("img").attr("src","/tk/img/Aleft-btn.png");
		$(this).siblings().children("img").attr("src","/tk/img/Bleft-btn.png");
		if($(this).attr("id")=="allList"){
			getLeftList();
			return false;
		}		
		$.ax(
				"/tk/api/resource/getAllResourceById",
				"typeid="+typeId,	//发送的数据
				null,
				"get",
				"json",
				function(data){
					var str = "";
						var obj = data;
						for(var i = 0 ; i <obj.length;i++){
							var time = getMyDate(obj[i].createtime);
							
							var intro = obj[i].introduction;
							if(intro=="null"||intro==undefined||intro==null||intro==""){
								intro="";
							}else{
								var strIndex = intro.indexOf("。");
								intro = intro.substring(0, strIndex+1);
							}
							if(obj[i].resource_type==1){
								//调用tools中的转换时间格式	
								
								str = str + "<li data-toggle='modal' data-target='.bs-example-modal-lg' context-id='"+obj[i].id+"'><p class='title-text'>"+obj[i].title+"</p><p class='content-text'>"+intro+"</p><p class='text-right small'>"+time+"</p>";
							}else if(obj[i].resource_type==3){
								str = str + "<li data-toggle='modal' data-target='.bs-example-modal-lg' class='row' context-id='"+obj[i].id+"'><img class='col-xs-12 col-sm-9 col-lg-9 col-md-9 left-img'  src='"+obj[i].resourceurl+"'</><div class='right-box col-xs-12 col-sm-3 col-lg-3 col-md-3 '><p class='title-text'>"+obj[i].title+"</p><p small'>"+time+"</p><p class='content-text'>"+intro+"</p></div>";
							}
						}
						$(".lists-ul").html(str);
						
				},
				function(){
					alert("error");
				},
				function(){
					$(".lists-ul").html("<img src='/tk/images/loading2.gif'/>");
				}
			);
	});
	
	$(document).click(function(){
		var active = $("#showAlert").css("display");
		//console.log("active:"+active);
		if(active=="block"){
			paser();
			//console.log("one");
		}
		//console.log("aa");
	})

	
	//点击列表 获取当前li的内容数据
	$(".lists-ul").on("click","li",function(){	
		var contextId = $(this).attr("context-id");
		console.info(contextId);
		$.ax(
			"/tk/api/resource/getByIdResource",
			"id="+contextId,
			true,
			"get",
			"json",
			function(data){
				var str = "";
					var obj = data;
						//调用tools中的转换时间格式
					var time = getMyDate(obj.createtime);
					var mess = "<span>发布时间："+time+"</span><span>点击数："+obj.clicknum+"</span>";
					str ="<p class='content-title'>"+obj.title+"</p><p class='content-message text-center'>"+ mess+"</p>"+ obj.context;
					
					$(".modal-body").html(str);
			},
			function(){
				alert("error");
			}
		);
	});
})

